<?

/*
Autor: Imanol Iglesias
email: imanolif@gmail.com
*/

include_once('ServiciosDisponibles.php');

class ParserSMS
{
	private $sms;
	private $servicio = null;
	private $location = null;
	private $query = null;
	
	public function __construct($sms)
	{
		$this->sms = $sms;
	}
	
	public function tieneTWT()
	{
		return (stripos($this->sms->getTexto(), 'TWT')!==false)?true:false;
	}
	
	public function getDatosTwitter()
	{
		if (!$this->tieneTWT())
			return '';
			
		$datos = explode('TWT', $this->sms->getTexto());
		$datos = explode(' ', trim($datos[1]));
		
		return array('usuario' => trim($datos[0]), 'password' => trim($datos[1]));
	}
	
	private function getServicio()
	{
		if ($this->servicio != null)
			return $this->servicio;
			
		$this->servicio = explode('TWT', $this->sms->getTexto());
		$this->servicio = trim($this->servicio[0]);
		
		return $this->servicio;
	}
	
	public function getLocation()
	{
		if ($this->location != null)
			return $this->location;
			
		$this->location = explode(' ', $this->getServicio($this->sms->getTexto()));
		$this->location = $this->location[count($this->location)-1];
		
		return $this->location;
	}
	
	public function getQuery()
	{
		if ($this->query != null)
			return $this->query;
			
		$palabras = explode(' ', $this->getServicio($this->sms->getTexto()));
		$this->query = '';
		for ($i = 0; $i < count($palabras) - 1; $i++)
		{
			$this->query .= $palabras[$i].' '; 
		}
		
		$this->query = trim($this->query);
		
		return $this->query;
	}
	
	public function getBusqueda()
	{
		$serviciosDisponibles = $GLOBALS['serviciosDisponibles'];
		
		$buscar = explode(' ', $this->getQuery());
		$buscar = ucfirst ($buscar['0']);
		
		if (in_array($buscar, $serviciosDisponibles))
			return $buscar;
			
		return $serviciosDisponibles['11870'];
	}
	

}

?>